package com.mischa.doedeljas.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import com.mischa.doedeljas.Card;
import com.mischa.doedeljas.Card.Suit;
import com.mischa.doedeljas.Messages;
import com.mischa.doedeljas.client.ui.CardButton;
import com.mischa.doedeljas.client.ui.CardsView;
import com.mischa.doedeljas.client.ui.CardsView.SelectionChangedListener;
import com.mischa.doedeljas.client.ui.ConnectionDialog;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DoedelJas extends javax.swing.JFrame implements
		SelectionChangedListener {	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6531966632915797171L;
	private CardsView _hand;
	private JPanel _followingCard;
	private JPanel _lastCard;
	private JPanel _myCard;

	private GameClient _game;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				DoedelJas inst = new DoedelJas();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public DoedelJas() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Doedeljas");
				{
					_myCard = new JPanel();
					getContentPane().add(_myCard, "cell 0 0");
					_myCard.setBounds(305, 119, 218, 88);
				}
				{
					_hand = new CardsView();
					getContentPane().add(_hand, "cell 0 1");
					_hand.setBounds(2, 219, 851, 105);
					_hand.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
							null, null, null, null));
					_hand.setSelectionChangedListener(this);
				}
				{
					_followingCard = new JPanel();
					getContentPane().add(_followingCard, "cell 0 0");
					_followingCard.setBounds(2, 35, 261, 97);
				}
				{
					_lastCard = new JPanel();
					getContentPane().add(_lastCard);
					_lastCard.setBounds(608, 35, 247, 90);
				}
			}
			this.setSize(865, 358);
			setDefaultCloseOperation(EXIT_ON_CLOSE);

			// Create the connection dialog
			ConnectionDialog connectionDialog = new ConnectionDialog(this);
			connectionDialog.setMainForm(this);
			connectionDialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void connect(String server, int port, int gameId)
			throws UnknownHostException, IOException {
		Socket socket = new Socket(server, port);
		_game = new GameClient(socket, this);
		_game.start();
		_game.sendMessage(Messages.JOIN, gameId);
	}

	public void showMessage(String message, String title, boolean isError) {
		JOptionPane.showMessageDialog(this, message, title,
				isError ? JOptionPane.ERROR_MESSAGE
						: JOptionPane.INFORMATION_MESSAGE);
	}

	public void handUpdated(Vector<Card> hand, Suit trump) {
		_hand.suspendLayout();
		_hand.clear();
		_hand.add(hand);
		_hand.setTrumpSuit(trump);
		_hand.resumeLayout();
	}

	@Override
	public void selectionChanged(CardsView cardsView, Card selectedCard) {
		if (cardsView == _hand) {
			_game.play(selectedCard);
		}
	}

	public void showTrick(int nextToPlay, Card[] cards) 
	{				
		JPanel[] cardsPlayed = new JPanel[] { _myCard, _followingCard, _lastCard };
		
		for (int i = 0; i < 3; ++i)
		{
			
			cardsPlayed[i].removeAll();				

			if (cards[i] != null)
			{
				try {
					cardsPlayed[i].add(new CardButton(cards[i]));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if (i == nextToPlay)
			{
				cardsPlayed[i].setBorder(BorderFactory.createRaisedBevelBorder());
			}
			else
			{
				cardsPlayed[i].setBorder(BorderFactory.createEmptyBorder());
			}
		}
		
		if (cards[0] != null)
		{				
			_hand.remove(cards[0]);
		}
				
		repaint();
	}
}
